﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;

/// <summary>
/// Business 的摘要说明
/// </summary>
public class Business
{
    public static string getNAString(object obj)
    {
        string result = "N/A";
        if (obj != null && (!string.IsNullOrEmpty(obj.ToString())))
        {
            result = obj.ToString();
        }
        return result;
    }

     public static int getFinLendReceiptCount(object Id)
    {
        int result=0;
         if (Id != null)
        {
            FinLendReceiptCollection finLendReceiptCollection = new FinLendReceiptCollection();
            finLendReceiptCollection.Query.Where(finLendReceiptCollection.Query.LendID == decimal.Parse(Id.ToString()));
            finLendReceiptCollection.Query.Load();
            result = finLendReceiptCollection.Count;
        }
        return result;
    }


     public static string getCodeTableValue(string code)
     {
         string result = string.Empty;
         if (!string.IsNullOrEmpty(code))
         {
             TSCodeTableCollection tSCodeTableCollection = new TSCodeTableCollection();
             tSCodeTableCollection.Query.Where(tSCodeTableCollection.Query.Code == code);
             tSCodeTableCollection.Query.Load();
             if (tSCodeTableCollection.HasData)
             {
                 result = tSCodeTableCollection[0].Value1;
             }
         }
         return result;
     }

     public static string getCompanyNo(string companyName, string companyCode)
     {
         string result = string.Empty;
         BFCompanyDicCollection bFCompanyDicCollection = new BFCompanyDicCollection();
         bFCompanyDicCollection.Query.Where(bFCompanyDicCollection.Query.CompanyName == companyName, bFCompanyDicCollection.Query.CompanyCode == companyCode);
         bFCompanyDicCollection.Query.Load();
         if (bFCompanyDicCollection.HasData)
         {
             result = bFCompanyDicCollection[0].CompanyNo;
         }
         return result;
     }

     public static int getBoxIndex(decimal Id, int year, string companyName, string companyCode)
     {
         FinBoxCollection finBoxCollection = new FinBoxCollection();
         finBoxCollection.Query.Where(finBoxCollection.Query.BoxID <= Id, finBoxCollection.Query.CreatedDttm <= DateTime.Parse(year.ToString() + "-12-31 23:59:59"),
             finBoxCollection.Query.CreatedDttm >= DateTime.Parse(year.ToString() + "-01-01 00:00:00"), finBoxCollection.Query.CompanyCode == companyCode,
             finBoxCollection.Query.CompanyName == companyName);
         finBoxCollection.Query.Load();
         return finBoxCollection.Count;
     }

     public static string getLendSN(decimal Id, int year)
     {
         FinLendCollection finLendCollection = new FinLendCollection();
         finLendCollection.Query.Where(finLendCollection.Query.LendID <= Id, finLendCollection.Query.CreatedDttm <= DateTime.Parse(year.ToString() + "-12-31 23:59:59"),
             finLendCollection.Query.CreatedDttm >= DateTime.Parse(year.ToString() + "-01-01 00:00:00"));
         finLendCollection.Query.Load();
         int index= finLendCollection.Count;
         return year.ToString() + (10000 + index).ToString().Substring(1, 4);
     }

     public static int getReceiptCount(decimal boxId)
     {
         FinBoxReceiptsQuery q1 = new FinBoxReceiptsQuery("q1");
         FinReceiptQuery q2 = new FinReceiptQuery("q2");
         q1.InnerJoin(q2).On(q1.ReceiptID==q2.ReceiptID);
         q1.Where(q1.BoxID == boxId, q2.BoxID == boxId, q2.IsDeleted == false);
         return q1.LoadDataTable().Rows.Count;
     }

     public static int getReceiptCount(object boxId)
     {
         int result = 0;
         if (boxId != null)
         {
             FinBoxReceiptsQuery q1 = new FinBoxReceiptsQuery("q1");
             FinReceiptQuery q2 = new FinReceiptQuery("q2");
             q1.InnerJoin(q2).On(q1.ReceiptID == q2.ReceiptID);
             q1.Where(q1.BoxID == boxId, q2.BoxID == boxId, q2.IsDeleted == false);
             return q1.LoadDataTable().Rows.Count;
         }
         return result;
     }


     public static string getReceiptIndexRange(object boxId)
     {
         string result = string.Empty;
         if (boxId != null)
         {
             decimal id = decimal.Parse(boxId.ToString());
             FinBox finBox = new FinBox();
             finBox.LoadByPrimaryKey(id);

             FinBoxReceiptsQuery q1 = new FinBoxReceiptsQuery("q1");
             FinReceiptQuery q2 = new FinReceiptQuery("q2");
             q1.InnerJoin(q2).On(q1.BoxID == q2.BoxID);

             switch (finBox.IndexType)
             {
                 case 1:
                     q1.Select(q2.DocumentsNo.As("Index")).Where(q1.BoxID == id, q2.IsDeleted == false).OrderBy(q2.DocumentsNo.Ascending);
                     break;
                 case 2:
                     q1.Select(q2.Reference.As("Index")).Where(q1.BoxID == id, q2.IsDeleted == false).OrderBy(q2.Reference.Ascending);
                     break;
                 case 3:
                     q1.Select(q2.ReferenceKey.As("Index")).Where(q1.BoxID == id, q2.IsDeleted == false).OrderBy(q2.ReferenceKey.Ascending);
                     break;
                 case 4:
                     q1.Select(q2.DocumentHeaderText.As("Index")).Where(q1.BoxID == id, q2.IsDeleted == false).OrderBy(q2.DocumentHeaderText.Ascending);
                     break;

             }
             DataTable dt = q1.LoadDataTable();
             if (dt.Rows.Count > 0)
             {
                 if ((dt.Rows[0]["Index"] != null) && (!string.IsNullOrEmpty(dt.Rows[0]["Index"].ToString())))
                 {
                     finBox.IndexStart = dt.Rows[0]["Index"].ToString();
                 }

                 if ((dt.Rows[dt.Rows.Count - 1]["Index"] != null) && (!string.IsNullOrEmpty(dt.Rows[dt.Rows.Count - 1]["Index"].ToString())))
                 {
                     finBox.IndexEnd = dt.Rows[dt.Rows.Count - 1]["Index"].ToString();
                 }
             }

             finBox.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
             finBox.UpdatedDttm = DateTime.Now;
             finBox.Save();
             result = finBox.IndexStart + "<br />~<br />" + finBox.IndexEnd;
             
             
             //
             //FinBoxReceiptsQuery q1 = new FinBoxReceiptsQuery("q1");
             //FinReceiptQuery q2 = new FinReceiptQuery("q2");
             //q1.InnerJoin(q2).On(q1.BoxID == q2.BoxID);
             //q1.Select(q2.DocumentsNo, q2.Reference, q2.ReferenceKey, q2.DocumentHeaderText).Where(q1.BoxID == id, q2.IsDeleted == false).OrderBy(q2.DocumentsNo.Ascending, q2.Reference.Ascending, q2.ReferenceKey.Ascending, q2.DocumentHeaderText.Ascending);
             //DataTable dt= q1.LoadDataTable();
             //if (dt.Rows.Count > 0)
             //{
             //    if ((dt.Rows[0]["DocumentsNo"] != null) && (!string.IsNullOrEmpty(dt.Rows[0]["DocumentsNo"].ToString())))
             //    {
             //        result = dt.Rows[0]["DocumentsNo"].ToString() + "<br />~<br />" + dt.Rows[dt.Rows.Count - 1]["DocumentsNo"].ToString();
             //    }
             //    else
             //    {
             //        if ((dt.Rows[0]["Reference"] != null) && (!string.IsNullOrEmpty(dt.Rows[0]["Reference"].ToString())))
             //        {
             //            result = dt.Rows[0]["Reference"].ToString() + "<br />~<br />" + dt.Rows[dt.Rows.Count - 1]["Reference"].ToString();
             //        }
             //        else
             //        {
             //            if ((dt.Rows[0]["ReferenceKey"] != null) && (!string.IsNullOrEmpty(dt.Rows[0]["ReferenceKey"].ToString())))
             //            {
             //                result = dt.Rows[0]["ReferenceKey"].ToString() + "<br />~<br />" + dt.Rows[dt.Rows.Count - 1]["ReferenceKey"].ToString();
             //            }
             //            else
             //            {
             //                if ((dt.Rows[0]["DocumentHeaderText"] != null) && (!string.IsNullOrEmpty(dt.Rows[0]["DocumentHeaderText"].ToString())))
             //                {
             //                    result = dt.Rows[0]["DocumentHeaderText"].ToString() + "<br />~<br />" + dt.Rows[dt.Rows.Count - 1]["DocumentHeaderText"].ToString();
             //                }
             //            }
             //        }
             //    }
             //}
         }
         return result;
     }


     public static string getReceiptIndexRange(object startMonth,object endMonth)
     {
         string result = string.Empty;
         if (startMonth!=null && (!string.IsNullOrEmpty(startMonth.ToString())))
         {
             result += startMonth.ToString();
         }

         if (endMonth != null && (!string.IsNullOrEmpty(endMonth.ToString())))
         {
             result += "-" + endMonth.ToString() + "月";
         }
         else
         {
             result += "月";
         }


         return result;
     }


     public static int getYear(object StoreStartDate)
     {
         int result = 0;

         if (StoreStartDate != null && IsDateTime(StoreStartDate.ToString()))
         {
             result = DateTime.Today.Year - 1 - DateTime.Parse(StoreStartDate.ToString()).Year;
             if (result < 0)
             { result = 0; }
         }

         return result;
     }


     public static bool IsDateTime(string str)
     {
         DateTime temp;
         return DateTime.TryParse(str, out temp);
     }

     public static string getCabinNumber(Guid Id)
     {
         BFThirdUnitNumber bFThirdUnitNumber = new BFThirdUnitNumber();
         bFThirdUnitNumber.LoadByPrimaryKey(Id);
         return bFThirdUnitNumber.Name;
     }

     public static DataTable SetShippingNo(DataTable dt)
     {
         var showShipId = ConstantValue.ShowCode.ShowShipID;
         dt.Columns.Add(showShipId);
         foreach (DataRow row in dt.Rows)
         {
             string shipId = row[FinShippingMetadata.ColumnNames.ShipID].ToString();
             if (shipId.Length < 3)
             {
                 var mergeStr = shipId.Length == 1 ? "00" : "0";
                 row[showShipId] = mergeStr + shipId;
             }
             else
             {
                 row[showShipId] = shipId;
             }
         }

         return dt;
     }


     

    //public static string getCompanyNo(string companyName,string companyCode)
    //{
    //    string result=string.Empty;
    //    BFCompanyDicCollection bFCompanyDicCollection = new BFCompanyDicCollection();
    //    bFCompanyDicCollection.Query.Select(bFCompanyDicCollection.Query.CompanyNo)
    //        .Where(bFCompanyDicCollection.Query.CompanyName == companyName, bFCompanyDicCollection.Query.CompanyCode==companyCode);
    //    bFCompanyDicCollection.Query.Load();
    //    if (bFCompanyDicCollection.Count ==1)
    //    {
    //        result = bFCompanyDicCollection[0].CompanyNo;
    //    }

    //    return result;
    //}
}